﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NETMovie.Entities;
using NETMovie.UI.WebMVC.Models;

namespace NETMovie.UI.WebMVC.Mappers
{
    public class UserMapper
    {
        public UserMapper()
        {
        }

        /// <summary>
        /// Maps the RegisterViewModel instance to a User entity instance.
        /// </summary>
        /// <param name="viewModel">The instance to be mapped</param>
        /// <returns>The mapped instance</returns>
        public User Map(RegisterViewModel viewModel)
        {
            if (viewModel == null)
                return null;

            User user = new User();
            user.UserName = viewModel.BasicInfo.UserName;
            user.Email = viewModel.BasicInfo.Email;
            user.AddressLine1 = viewModel.AddressLine1;
            user.AddressLine2 = viewModel.AddressLine2;
            user.City = viewModel.City;
            user.Country = new Country();
            user.Country.CountryCode = viewModel.Country;
            user.PostalCode = viewModel.PostalCode;

            return user;
        }

        /// <summary>
        /// Maps the User instance to a EditProfileViewModel instance.
        /// </summary>
        /// <param name="user">The instance to be mapped</param>
        /// <returns>The mapped instance</returns>
        public EditProfileViewModel ReMap(User user)
        {
            if (user == null)
                return null;

            EditProfileViewModel model = new EditProfileViewModel();
            model.UserName = user.UserName;
            model.Email = user.Email;
            model.AddressLine1 = user.AddressLine1;
            model.AddressLine2 = user.AddressLine2;
            model.City = user.City;
            model.Country = user.Country.CountryCode;
            model.PostalCode = user.PostalCode;

            return model;
        }

        /// <summary>
        /// Maps the EditProfileViewModel instance to a User entity instance.
        /// </summary>
        /// <param name="viewModel">The instance to be mapped</param>
        /// <returns>The mapped instance</returns>
        public User Map(EditProfileViewModel model)
        {
            if (model == null)
                return null;

            User user = new User();
            user.UserName = model.UserName;
            user.Email = model.Email;
            user.AddressLine1 = model.AddressLine1;
            user.AddressLine2 = model.AddressLine2;
            user.City = model.City;
            user.Country = new Country();
            user.Country.CountryCode = model.Country;
            user.PostalCode = model.PostalCode;

            return user;
        }
    }
}